


const query = {
    page: 1, // 是	int	页码值
    limit: 10, // 是	int	每页显示多少条数据
    cate_id: "", // "" 所有的文章分类 否	string	文章分类的 Id
    state: "", // "" 所有状态 文章状态  可选值有：已发布、草稿
    catename:''
};
let form = layui.form
let laypage = layui.laypage;

function getRander() {
    axios({
        method: 'get',
        url: '/ydd_cate/list',
        params: query,
    }).then(({ data: { data: res } }) => {
        const { data, count } = res
        const htmlStr = data.map(({ default_data, id, catename, icon, sort_num }, i) => {
            return ` <tr>
                        <td>${i + 1}</td>
                        <td class="xxx">${id}</td>
                        <td class="yyy">${catename}</td>
                        <td class="zzz">${icon}</td>
                        <td class="ccc">${sort_num}</td>
                        <td class="qqq">
                        <button class="layui-btn layui-btn-sm layui-btn-radius layui-btn-normal bj" data-id=${id}>
                            <i class="layui-icon layui-icon-edit"></i>  
                        </button>
                        <button class="layui-btn layui-btn-sm layui-btn-radius layui-btn-danger sc" data-id=${id}>
                            <i class="layui-icon layui-icon-delete"></i>  
                        </button>
                        </td>
                    </tr>
            `
        }).join('')
        $('tbody').html(htmlStr)
        randerPage(count)
    })
}
getRander()

//搜索
$('.ss').click(function () {
    const params = $('[name="title"]').val()
    query.catename = params
    query.page = 1
    $('[name="title"]').val('')
    getRander()
})

//重置
$('.cc').click(function () {
    const params = $('[name="title"]').val()
    query.catename = params
        getRander()
})



//点击编辑
$('tbody').on('click', '.bj', function (e) {
    e.preventDefault()
    const id = $(this).attr('data-id')
    axios({
        method: 'get',
        url: `/ydd_cate/rowInfo?id=${id}`,
    }).then(({ data: { data } }) => {
        form.val("formEdit", data);
    })

    layer.open({
        title: '修改分类'
        , content: $('#form').html(),
        area: ['800px', '350px'],
        btn: ['确定', '取消'],
        yes: function (index, layero) {
            //按钮【按钮一】的回调
            // console.log(layero[0]);
            var data = $("#edit").serialize()
            console.log(data);
            axios({
                method: 'post',
                url: '/ydd_cate/update',
                data: data
            }).then(({ data: { errmsg, errno } }) => {
                // console.log(res);
                if (errno !== 0) return layer.msg(errmsg)
                layer.msg('修改成功')
                getRander()
            })
            return false
        },
        btn2: function () {
            //按钮【按钮二】的回调
            //return false 开启该代码可禁止点击该按钮关闭
        }
    });
})

//新增
$('.xinzeng').click(function () {
    layer.open({
        title: '修改分类'
        , content: $('#addform').html(),
        area: ['800px', '350px'],
        btn: ['确定', '取消'],
        yes: function (index, layero) {
            //按钮【按钮一】的回调
            // console.log(layero[0]);
            var data = $("#add").serialize()
            axios({
                method: 'post',
                url: '/ydd_cate/add',
                data: data
            }).then(({ data: { errmsg, errno } }) => {
                // console.log(res);
                if (errno !== 0) return layer.msg(errmsg)
                layer.msg('添加成功')
                getRander()
                // layer.close(indexopen)
            })
            // console.log(data);
            return false
        },
        btn2: function () {
            //按钮【按钮二】的回调
            //return false 开启该代码可禁止点击该按钮关闭
        }
    });
})


//点击删除
$('tbody').on('click', '.sc', function (e) {
    e.preventDefault()
    const id = $(this).attr('data-id')
    console.log(id);
    axios({
        method: 'get',
        url: `/ydd_cate/del?id=${id}`,
    }).then(({ data: { errno, errmsg } }) => {
        console.log(errno, errmsg);
        if (errno !== 0) return layer.msg(errmsg)
        layer.msg('删除成功')
        getRander()
    })
})



let i = 0
$('body').on('click', '.jian', function () {
    i = $('[name="sort_num"]').val()
    i--
    if (i < 0) {
        return
    }
    $('[name="sort_num"]').val(i--)
})

$('body').on('click', '.jia', function () {
    i = $('[name="sort_num"]').val()
    i++
    $('[name="sort_num"]').val(i++)
})

//右边栏三个小图标
$('.one').click(function () {
    $('.sousuo').slideToggle()
    // console.log($(this).html(i));
    $('.one_1').toggle().siblings().toggle()
})
$('.tow').click(function () {
    $('[name="ID"]').prop('checked',true)
    $('[name="flmc"]').prop('checked',true)
    $('[name="tb"]').prop('checked',true)
    $('[name="qz"]').prop('checked',true)
    $('.xxx').show()
    $('.yyy').show()
    $('.zzz').show()
    $('.ccc').show()
    const params = $('[name="title"]').val()
    query.catename = params
    getRander()
})
$('.three').click(function () {
    $('.hezi').fadeToggle()
})



    $('body').on('change','.hezi',function(){
        $('[name="ID"]').prop('checked') ?   $('.xxx').show() :  $('.xxx').hide()
        $('[name="flmc"]').prop('checked') ?   $('.yyy').show() :  $('.yyy').hide()
        $('[name="tb"]').prop('checked') ?   $('.zzz').show() :  $('.zzz').hide()
        $('[name="qz"]').prop('checked') ?   $('.ccc').show() :  $('.ccc').hide()
    })

layui.use('form', function () {
    form.render()
    //各种基于事件的操作，下面会有进一步介绍

});

//分页
function randerPage(count) {
    //执行一个laypage实例
    laypage.render({
        elem: 'page-box', //注意，这里的 test1 是 ID，不用加 # 号
        count: count,//数据总数，从服务端得到
        limit: query.limit,
        limits: [1, 2, 3, 4, 5, 6, 10, 13],
        curr: query.page,
        layout: ['count', 'limit', 'prev', 'page', 'next', 'skip'],
        theme:'#409eff',
        jump: function (obj, first) {
            //   页码数要随着变化
            query.page = obj.curr
            query.limit = obj.limit
            if (!first) {
                getRander()
            }
        }
    });
}





function download(filename, content) {
    let blob = new Blob([content], {type: 'text/html'});
    console.log(blob);
    let url = window.URL.createObjectURL(blob);
    let a = document.createElement('a');

    a.style = "display: none";
    a.href = url;
    a.download = filename;
    document.body.appendChild(a);
    a.click();

    setTimeout(function () {
        document.body.removeChild(a);
        window.URL.revokeObjectURL(url);
    }, 5);
}

document.getElementById('down').onclick = function () {
    // console.log(111);
    let filename = "分类管理";
    let content = $('body').html();
    download(filename, content);
};

